<?php
if (!defined("LEVEL") || LEVEL < 20 || !defined("OK")) { header('location: http://'.$_SERVER["HTTP_HOST"].''); exit; }
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = escape(ceil((int)$url['p'])); } else { $p = 0; }
$limit = 30;
$viso = kiek('admin_chat');

unset($extra);
if (isset($_POST['admin_chat_send']) && $_POST['admin_chat_send'] == 'Siųsti') {
	//printf("<pre>%s</pre>",print_r($_POST,true));
	//Irasom zinute
	if (isset($_POST['pm']) && is_array($_POST['pm'])) {
		$extra = "[i]Globalus pranešimas grupei:[b]". implode(",", $_POST['pm'])."[/b][/i]\n---\n";
		foreach($_POST['pm'] as $key => $val) {
			$sql = mysql_query1("SELECT `nick` FROM `".LENTELES_PRIESAGA."users` WHERE levelis = $val");
			while ($row = mysql_fetch_assoc($sql)) {
				mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."private_msg` (`from` , `to` , `title` , `msg` , `date`) VALUES (".escape($_SESSION['username']).", ".escape($row['nick']).", 'Administracija praneša!', ".escape($_POST['admin_chat']).", NOW())");
			}
		}
	}
	elseif ($_POST['pm'] == 1) {
		$extra = "[i]Globalus pranešimas: [b]visiems[/b][/i]\n---\n";
		$sql = mysql_query1("SELECT `nick` FROM `".LENTELES_PRIESAGA."users`");
		while ($row = mysql_fetch_assoc($sql)) {
			mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."private_msg` (`from` , `to` , `title` , `msg` , `date`) VALUES (".escape($_SESSION['username']).", ".escape($row['nick']).", 'Administracija praneša!', ".escape($_POST['admin_chat']).", NOW())");
		}
	}
	mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."admin_chat` (admin, msg, date) VALUES(".escape($_SESSION['username']).",".escape($extra.$_POST['admin_chat']).",NOW())") or die(mysql_error());
	header("Location: ?id,46");
}
//trinam zinute
if (isset($url['d']) && !isset($url['a']) && isnum($url['d']) && $url['d'] > 0 && LEVEL == 30) {
	mysql_query1("DELETE FROM `".LENTELES_PRIESAGA."admin_chat` WHERE id=".(int)$url['d']."");
	header("Location: ?id,46");
}
//else { klaida('Atsiprašome', 'Jums nesuteiktos teisės trinti administratorių pranešimus'); }
//redaguojam zinute
if (isset($url['r']) && !isset($url['d']) && !isset($url['a']) && isnum($url['r']) && $url['r'] > 0) {
	if (!isset($_POST['admin_chat_send'])) { $extra = mysql_fetch_assoc(mysql_query1("SELECT msg FROM `".LENTELES_PRIESAGA."admin_chat` WHERE id=".escape((int)$url['r'])." LIMIT 1")); $extra = $extra['msg']; }
	elseif ($_POST['admin_chat_send'] == "Redaguoti") {
		mysql_query("UPDATE `".LENTELES_PRIESAGA."admin_chat` SET `msg`=".escape($_POST['admin_chat']).",`date` = NOW() WHERE `admin`=".escape($_SESSION['username'])." AND id=".escape((int)$url['r'])." LIMIT 1");
		header("Location: ?id,46");
	}
}
$text = "
		<form name=\"admin_chat\" action=\"\" method=\"post\" id=\"chat\">
		<fieldset style='padding:3px'><legend>Kartu siųsti PM:</legend>
		<label><input type=\"checkbox\" name=\"pm[]\" value=\"30\" tabindex=\"1\" />Adminams</label>
		<label><input type=\"checkbox\" name=\"pm[]\" value=\"20\" tabindex=\"2\" />Visiem DJ'ėjams</label>
		<label><input type=\"checkbox\" name=\"pm[]\" value=\"10\" tabindex=\"3\" />Vartotojams</label>
		<label><input type=\"checkbox\" name=\"pm\" value=\"1\" tabindex=\"4\" />Visiems</label>
		</fieldset>
		<center>
		<br/>
		<textarea name=\"admin_chat\" rows=7 cols=\"5\" style='width:100%'>".((isset($extra) && isset($url['r']))?input($extra):'')."</textarea>
		<br/>
		" . bbk("admin_chat") ."
        <br/>
        <input name=\"admin_chat_send\" type=\"submit\" value=\"".((isset($url['r']) && isset($extra))?'Redaguoti':'Siųsti')."\">
		</form>
		</center><br/>";
//hide("Rašyti pranešimą",$text,false,false,"admin_chat"); $text = '';
//puslapiavimas
if ($viso > $limit) { lentele("Puslapiai:",puslapiai($p,$limit,$viso,10)); }
$sql = mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."admin_chat` ORDER BY date DESC LIMIT ".escape($p).",".$limit."");
while ($row = mysql_fetch_assoc($sql)) {
		$text .= "
				<div class='title'><em><a href=\"".url("d,".$row['id']."")."\" onclick=\"return confirm('Ar tikrai norite trinti?')\">[d]</a> ".(($_SESSION['username'] == $row['admin'])?"<a href=\"".url("r,".$row['id']."")."\">[r]</a> ":"") . $row['admin'] ." [" . $row['date'] ."] - ".kada($row['date'])." ".naujas($row['date'],$row['admin'])."</em></div>
				<blockquote>" . bbcode($row['msg']) ."</blockquote><br/>
		";
}
lentele("Administratorių Pokalbiai",$text,false,array('Viso',$viso));
if ($viso > $limit) { lentele("Puslapiai:",puslapiai($p,$limit,$viso,10)); }
//$text = '';
?>